package com.shop.cereshop.app.page.recommend;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shop.cereshop.commons.domain.recommend.CereRecommendType;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.TimeUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Data
@ApiModel(value = "RecommendPageVO", description = "动态分页数据")
@Accessors(chain = true)
@Slf4j
public class RecommendPageVO{

    //动态相关字段
    @ApiModelProperty(value = "动态id")
    private Long recommendId;

    @ApiModelProperty(value = "草稿箱id")
    private Long draftId;

    @ApiModelProperty(value = "话题id集合字符串")
    @JsonIgnore
    private String recommendTypeIds;

    @ApiModelProperty(value = "当前话题id")
    @JsonIgnore
    private Long currentTypeId;

    @ApiModelProperty(value = "话题名称集合字符串")
    @JsonIgnore
    private String recommendTypeNames;

    @ApiModelProperty(value = "话题列表")
    private List<CereRecommendType> recommendTypeList;

    @ApiModelProperty(value = "文件类型(1-图片，2-视频)")
    private Integer fileType;

    @ApiModelProperty(value = "封面(只有视频才返回)")
    private String cover;

    @ApiModelProperty(value = "文件地址")
    private String fileUrl;

    @ApiModelProperty(value = "文件id")
    private String mediaId;

    @ApiModelProperty(value = "文案")
    private String remark;

    @ApiModelProperty(value = "点赞数")
    private Integer likeCount;

    @ApiModelProperty(value = "点赞状态：0-未点赞，1-已点赞")
    private Integer likeStatus;

    @ApiModelProperty(value = "收藏数")
    private Integer collectCount;

    @ApiModelProperty(value = "收藏状态：0-未收藏，1-已收藏")
    private Integer collectStatus;

    @ApiModelProperty(value = "评论数")
    private Integer commentCount;

    @ApiModelProperty(value = "浏览数")
    private Integer browseCount;


    //发布者信息相关
    @ApiModelProperty(value = "发布者id")
    private Long publisherId;

    @ApiModelProperty(value = "发布者名称")
    private String publisherName;

    @ApiModelProperty(value = "发布者头像")
    private String publisherImage;

    @ApiModelProperty(value = "发布时间")
    private String publishTime;

    @ApiModelProperty(value = "创建时间")
    @JsonIgnore
    private LocalDateTime createTime;

    @ApiModelProperty(value = "发布时间(时间戳,草稿箱的创建时间)")
    private String publishTimes;

    @ApiModelProperty(value = "动态可见条件")
    private Integer visitType;

    @ApiModelProperty(value = "动态发布省")
    private String province;

    @ApiModelProperty(value = "动态发布市")
    private String city;

    @ApiModelProperty(value = "动态发布详细地址")
    private String detailAddress;

    @ApiModelProperty(value = "动态发布经纬度度")
    private String location;

    @ApiModelProperty(value = "动态发布@用户信息")
    private List<AtUserVO> atUser;

    @ApiModelProperty(value = "动态发布@用户信息字符串")
    @JsonIgnore
    private String atUsers;

    @ApiModelProperty(value = "动态发布是否置顶")
    private Boolean pinnedStatus = false;

    @ApiModelProperty(value = "关注状态：0-未关注，1-已关注")
    private Integer concernStatus;

    @ApiModelProperty(value = "动态发布状态：0-未审核 1-审核成功 2-审核失败")
    private Integer publishStatus;

    public List<AtUserVO> toAtUser() {
        if (StrUtil.isNotBlank(atUsers)) {
            atUser = Arrays.stream(this.atUsers.split(","))
                    .map(pair -> {
                        String[] parts = pair.split(":");
                        if (parts.length == 2) {
                            AtUserVO atUserVO = new AtUserVO();
                            atUserVO.setAtUserId(Long.valueOf(parts[0]));
                            atUserVO.setAtUserName(parts[1]);
                            return atUserVO;
                        }
                        return null;
                    })
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            return atUser;
        }
        return Collections.emptyList();
    }

    public void toRecommendTypeList() {
        if (StrUtil.isNotBlank(this.recommendTypeIds) && StrUtil.isNotBlank(this.recommendTypeNames)) {
            String[] ids = this.recommendTypeIds.split(",");
            String[] names = this.recommendTypeNames.split(",");

            // 确保两个数组长度一致
            int length = Math.min(ids.length, names.length);

            this.recommendTypeList = new ArrayList<>(length);
            for (int i = 0; i < length; i++) {
                try {
                    CereRecommendType type = new CereRecommendType();
                    type.setRecommendTypeId(Long.valueOf(ids[i].trim()));
                    type.setName(names[i].trim());
                    this.recommendTypeList.add(type);
                } catch (NumberFormatException e) {
                    // 忽略无法解析的ID
                }
            }
        } else {
            this.recommendTypeList = Collections.emptyList();
        }
    }

    public void init() {
        // 处理ID和名称列表
        if (!EmptyUtils.isEmpty(this.recommendTypeIds) && !EmptyUtils.isEmpty(this.recommendTypeNames)) {
            this.toRecommendTypeList();
        }
        if (!EmptyUtils.isEmpty(this.publishTime)){
            this.setPublishTimes(TimeUtils.toRelativeTime(this.publishTime));
        }
        if (!EmptyUtils.isEmpty(this.atUsers)) {
            this.setAtUser(this.toAtUser());
        }
    }
}
